import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/double_control/get_target_tasking_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/danger_investigation_page/task_allocation_hazard_investigation/task_assigned/task_assigned_controller.dart';

///任务分配已分配
class TaskAssignedPage extends StatelessWidget {
  TaskAssignedPage({super.key});

  final controller = Get.put(TaskAssignedController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            key: controller.riskRightScaffoldKey,
            body: Container(
              color: AppColors.bg_color_grey,
              padding: EdgeInsets.only(top: 12.w, right: 14.w, left: 14.w),
              child: Column(
                children: [
                  Row(
                    children: [
                      Expanded(
                        child: _getSearchEnterWidget(),
                      ),
                      AppGaps.hGap8,
                      _screen(),
                      AppGaps.vGap10,
                    ],
                  ),
                  AppGaps.vGap12,
                  Expanded(
                      child: CustomEasyRefreshList<GetTargetTaskingEntity>(
                    key: controller.globalKey,
                    dataLoad: controller.getTargetTaskingAPP,
                    itemBuilder: (item, index) {
                      return CustomTitleChartPage(
                        title: item.troubleshootContent ?? '',
                        titleRightWidget: _title(item, '移除', Colors.white),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            item.workKindLabel == '季节性排查' || item.workKindLabel == '重点时段及节假日前排查'
                                ? Row(
                                    children: [
                                      _rowData(mainAxisAlignment: MainAxisAlignment.start, '${item.workKindLabel}  |  ', ''),
                                      Row(
                                        children: item.workKindCycleChangeList != null
                                            ? item.workKindCycleChangeList!
                                                .map((e) => _rowData(
                                                    mainAxisAlignment: MainAxisAlignment.start, '${e.checkCycle ?? '空'}${e.checkCycleUnitLabel ?? '空'} ', ''))
                                                .toList()
                                            : [],
                                      )
                                    ],
                                  )
                                : _rowData('${item.workKindLabel}  |  ${item.checkCycle ?? '空'}${item.checkCycleUnitLabel ?? '空'}', ''),
                            AppGaps.vGap12,
                            _rowData('部门：', item.allocationName ?? '空'),
                            AppGaps.vGap12,
                          ],
                        ).clickOnce(() {
                          controller.onClickDetail(item.id);
                        }),
                      );
                    },
                  )),
                ],
              ),
            ), // 右侧筛选栏
            // endDrawer:CustomEndDrawer(
            //   centerContext: Column(
            //     children: [
            //       _selectType('任务类别', controller.workKindNameBack ?? ''),
            //        DepartmentRegionalOrganizationTreePage(
            //         selectedEntity: controller.selectedEntity,
            //         onCallBack: (DepartmentUserTreeEntity entity) {
            //           controller.selectedEntity = entity;
            //         },
            //       ),
            //     ],
            //   ),
            //   reset: controller.reset,
            //   confirm: controller.confirm,
            // ),
          );
        });
  }

  ///搜索框
  Widget _getSearchEnterWidget() {
    return Container(
        height: 38.w,
        padding: EdgeInsets.symmetric(horizontal: 12.w),
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: Colors.white, border: Border.all(color: const Color(0xffF0F0F0), width: 1.w), borderRadius: BorderRadius.all(Radius.circular(8.w))),
        child: Row(
          children: [
            SvgPicture.asset(
              AppImages.icon_search,
              width: 16.w,
            ),
            Expanded(
                child: TextField(
                    keyboardType: TextInputType.text,
                    textInputAction: TextInputAction.done,
                    controller: TextEditingController(text: controller.searchX),
                    onChanged: (value) {
                      controller.searchX = value;
                      controller.refreshPage();
                    },
                    style: TextStyle(color: const Color(0xff222222), fontWeight: FontWeight.w400, fontSize: 14.sp),
                    decoration: InputDecoration(
                      counterText: '',
                      //防止适配不居中
                      isCollapsed: true,
                      border: const OutlineInputBorder(borderSide: BorderSide.none),
                      contentPadding: EdgeInsets.only(left: 8.w),
                      hintText: '搜索任务名称',
                      hintStyle: TextStyle(fontSize: 14.sp, fontWeight: FontWeight.w400, color: const Color(0xffA0A7B5)),
                    )))
          ],
        ));
  }

  ///筛选
  Widget _screen() {
    return Container(
      width: 38.w,
      height: 38.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
      child: SvgPicture.asset(
        AppImages.icon_search_screen,
        width: 16.w,
      ),
    ).click(controller.onFilterButtonPressed);
  }

  Widget _title(GetTargetTaskingEntity item, String title, Color color) {
    return Stack(
      alignment: Alignment.center,
      children: [
        SvgPicture.asset(AppImages.icon_task_assigned_bg),
        Text(
          title,
          style: TextStyle(
            color: color,
            fontSize: 10.sp,
            fontWeight: FontWeight.w400,
          ),
        ),
      ],
    ).clickOnce(() {
      controller.delAllocationBaseMeasures(item.id);
    });
  }

  Widget _rowData(String name, String value, {MainAxisAlignment mainAxisAlignment = MainAxisAlignment.spaceBetween}) {
    return Row(
      mainAxisAlignment: mainAxisAlignment,
      children: [
        Text(
          name,
          style: TextStyle(
            color: const Color(0xFFA5A6B5),
            fontWeight: FontWeight.w400,
            fontSize: 12.sp,
          ),
        ),
        Text(
          value,
          style: TextStyle(
            color: const Color(0xFF495061),
            fontSize: 12.sp,
            fontWeight: FontWeight.w400,
          ),
        )
      ],
    );
  }
}
